﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using WssUtils;
using WssUtils.EasyCSV;

namespace JQMergeFramework
{
    public class TableConfig : Singleton<TableConfig>
    {
        //关卡配置信息
        private IDictionary<int, LevelCfg> _levelCfgInfos = new Dictionary<int, LevelCfg>();
        private IDictionary<int, CityTargetCfg> _cityTargetCfgInfos = new Dictionary<int, CityTargetCfg>();
        private List<CityTargetCfg> _cityTargetCfgList = new List<CityTargetCfg>();
        
        
        private IDictionary<int, NumericalCfg> _numericalCfgInfos = new Dictionary<int, NumericalCfg>();
        private List<NumericalCfg> _numericalCfgInfosList = new List<NumericalCfg>();
        private IList<MarqueeCfg> _marqueeCfgList = new List<MarqueeCfg>();
        
        
        public IList<MarqueeCfg> MarqueeCfgList
        {
            get { return _marqueeCfgList; }
        }

        //成就
        public List<CityTargetCfg> CityTargetCfgList
        {
            get { return _cityTargetCfgList; }
        }
        
        public void Init()
        {
            //初始化表格
            CsvHelper.Init(',');

            CSVRoot _csvRoot = Resources.Load<ScriptableObject>("CSVRoot") as CSVRoot;
            foreach (var item in _csvRoot.textAssets)
            {
                if (CsvHelper.Get(item.name) == null)
                {
                    CsvHelper.Create(item.name, item.text);
                }
            }

            IDictionary<string, LevelCfg> tbSkinProperty = CsvHelper.ConvertToDictionary<LevelCfg>("LvCfg");
            foreach (LevelCfg item in tbSkinProperty.Values)
            {
                _levelCfgInfos.Add(item.id,item);
            }
            
            IDictionary<string, CityTargetCfg> tbCityTargetCfg = CsvHelper.ConvertToDictionary<CityTargetCfg>("CityTargetCfg");
            foreach (CityTargetCfg item in tbCityTargetCfg.Values)
            {
                _cityTargetCfgInfos.Add(item.lv,item);
            }
            
            _cityTargetCfgList = tbCityTargetCfg.Values.ToList();
            
            
            IDictionary<string, TempNumericalCfg> tbNumericalCfg = CsvHelper.ConvertToDictionary<TempNumericalCfg>("Numerical");
            foreach (TempNumericalCfg item in tbNumericalCfg.Values)
            {
                int targetScore = (int)MathF.Floor(item.targetScore);
                int addScore = (int)MathF.Floor(item.addScore);
                _numericalCfgInfos.Add(item.id,new NumericalCfg(item.id,item.curve,item.expectPlace,item.averageCityLv,item.maxLv,item.addCityNum,targetScore,addScore));
            }

            _marqueeCfgList = CsvHelper.ConvertToList<MarqueeCfg>("MarqueeCfg");
            
            _numericalCfgInfosList = _numericalCfgInfos.Values.ToList();
        }

        public LevelCfg GetLevelCfg(int lv)
        {
            if (_levelCfgInfos.ContainsKey(lv))
            {
                return _levelCfgInfos[lv];
            }
            return null;
        }

        
        public CityTargetCfg GetTargetCfg(int lv)
        {
            for (int i = 0; i <  _cityTargetCfgList.Count; i++)
            {
                if (_cityTargetCfgList[i].lv > lv)
                {
                    return _cityTargetCfgList[i];
                }
            }
            return _cityTargetCfgList[_cityTargetCfgList.Count];;
        }

        
        public CityTargetCfg GetFinishedTargetCfg(int lv)
        {
            for (int i = _cityTargetCfgList.Count-1; i >=  0; i--)
            {
                if (_cityTargetCfgList[i].lv <= lv)
                {
                    return _cityTargetCfgList[i];
                }
            }
            return null;
        }
        
        //获取成就的完成数量
        public int GetFinishedTargetNum(int lv)
        {
            int num = 0;
            for (int i = _cityTargetCfgList.Count-1; i >=  0; i--)
            {
                if (_cityTargetCfgList[i].lv <= lv)
                {
                    num++;
                }
            }
            return num;
        }

        //获取数值
        public NumericalCfg GetNumericalCfg(int lv)
        {
            if (_numericalCfgInfos.ContainsKey(lv))
            {
                return _numericalCfgInfos[lv];
            }
            return _numericalCfgInfos[_numericalCfgInfos.Count+1];
        }
        
    }
}